package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.MyHouseRecyclerAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.MyHouseBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 我的房产界面，处理房产
 *
 * @author fengzhen
 * @version v1.0, 2018/4/13
 */
public class MyHouseActivity extends BasePersonActivity {

    @BindView(R.id.recycler_activity_my_house)
    RecyclerView mRecycler;

    private LoadingPagerHead mLoadingPagerHead;
    private List<MyHouseBean> mHouseList = new ArrayList<>();
    private MyHouseRecyclerAdapter mHouseAdapter;

    /**
     * 跳转我的房产界面
     */
    public static Intent call2MyHouseActivity(Context context) {
        Intent intent = new Intent(context, MyHouseActivity.class);
        return intent;
    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("我的房产");

        mRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mHouseAdapter = new MyHouseRecyclerAdapter(this, mHouseList);
        mRecycler.setAdapter(mHouseAdapter);

        getMyHouse();
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected View getContentView(Context context) {
        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {
                onBackPressed();
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(MyHouseActivity.this, R.layout.activity_my_house);
            }

            @Override
            protected void reLoading() {
                getMyHouse();
            }
        };
        return mLoadingPagerHead;
    }

    @Override
    protected void returnMainActivity(View v) {
        onBackPressed();
    }

    private void getMyHouse() {
        mLoadingPagerHead.showPagerView(Contans.STATE_LOADING);
        Observable<StatusCode<List<MyHouseBean>>> observable = ApiUtils.getApi().getMyHouses(BaseApplication.getToken(), BaseApplication.getUserId());
        HttpUtil.getInstance().toSubscribe(observable,
                new ProgressSubscriber<List<MyHouseBean>>(this) {
                    @Override
                    protected void _onNext(StatusCode<List<MyHouseBean>> statusCode) {
                        if (statusCode.getData() == null) {
                            mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                            return;
                        }
                        mHouseList.clear();
                        mHouseList.addAll(statusCode.getData());
                        mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                        mHouseAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void _onError(String message) {
                        mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                    }
                }, "", lifecycleSubject, false, true);
    }
}
